package org.hank.compose_yyzs.ui.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.orhanobut.logger.Logger
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator
import org.hank.compose_yyzs.R
import org.hank.compose_yyzs.ui.destinations.LoginScreenDestination
import org.hank.compose_yyzs.ui.destinations.TipsDialogDestination
import org.hank.compose_yyzs.ui.theme.AppTheme
import org.hank.compose_yyzs.ui.widget.dialog.BottomUpdatePassDialog
import org.hank.compose_yyzs.ui.widget.dialog.TipsDialog
import org.hank.compose_yyzs.utils.getString
import org.hank.compose_yyzs.viewmodel.UserInfoViewEvent
import org.hank.compose_yyzs.viewmodel.UserInfoViewModel

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/9/13 10:23
 *描述:
 */

@Composable
@Destination
fun UserInfoScreen(navigation: DestinationsNavigator) {
    var viewModel: UserInfoViewModel = hiltViewModel()
    var userName = viewModel.userName.collectAsStateWithLifecycle()
    var userPhone = viewModel.userPhone.collectAsStateWithLifecycle()
    var userEmail = viewModel.userEmail.collectAsStateWithLifecycle()
    var lifecycleOwner = LocalLifecycleOwner.current
    var tipsUpdateSuccess = remember { mutableStateOf(false) }
    var showChangePass = remember { mutableStateOf(false) }

    DisposableEffect(lifecycleOwner) {
        val observer = LifecycleEventObserver { _, event ->
            if (event == Lifecycle.Event.ON_CREATE) {
                viewModel.getUserInfo()
            }
        }
        lifecycleOwner.lifecycle.addObserver(observer)
        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
        }
    }

    LaunchedEffect(Unit) {
        viewModel.viewEvents.collect {
            Logger.d("collect userinfo by UserInfoCompose")
            if (it is UserInfoViewEvent.ErrMessage) {
                navigation.navigate(TipsDialogDestination(it.errMessage))
            }
            if (it is UserInfoViewEvent.UpdatePassSuccess) {
                tipsUpdateSuccess.value = true
            }
        }
    }

    if(tipsUpdateSuccess.value)
        TipsDialog(tipsString = getString(resourceId = R.string.userinfo_update_success),false){
            showChangePass.value = false
            navigation.navigate(LoginScreenDestination.route){
                popUpTo(LoginScreenDestination.route){
                    inclusive = true
                }
            }
        }

    Column(
        modifier = Modifier
            .fillMaxSize()
    ) {
        Surface(shadowElevation = 4.dp) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(color = AppTheme.colors.mainColor)
            ) {
                Box(modifier = Modifier.fillMaxWidth()) {
                    Image(
                        painter = painterResource(id = R.mipmap.ic_return_black),
                        contentDescription = "",
                        modifier = Modifier
                            .align(Alignment.CenterStart)
                            .padding(16.dp)
                            .clickable(
                                indication = null,
                                onClick = { navigation.popBackStack() },
                                interactionSource = remember { MutableInteractionSource() })
                    )
                    Text(
                        getString(R.string.userinfo_title),
                        modifier = Modifier
                            .align(Alignment.Center)
                            .padding(top = 16.dp, bottom = 16.dp),
                        style = MaterialTheme.typography.titleLarge,
                    )
                }
                Divider(color = AppTheme.colors.divider, thickness = 1.dp)
            }
        }


        Spacer(modifier = Modifier.height(12.dp))
        Surface(shadowElevation = 4.dp) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(color = AppTheme.colors.mainColor)
            ) {

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp), verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = getString(R.string.userinfo_name),
                        modifier = Modifier.weight(1f)
                    )
                    Text(
                        text = userName.value,
                        style = MaterialTheme.typography.bodyMedium.copy(color = AppTheme.colors.textSecondary)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Image(
                        painter = painterResource(id = R.mipmap.ic_into_grey),
                        contentDescription = ""
                    )
                }
                Divider(
                    color = AppTheme.colors.divider,
                    thickness = 1.dp,
                    modifier = Modifier.padding(start = 16.dp, end = 16.dp)
                )

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp), verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = getString(R.string.userinfo_phone),
                        modifier = Modifier.weight(1f)
                    )
                    Text(
                        text = userPhone.value,
                        style = MaterialTheme.typography.bodyMedium.copy(color = AppTheme.colors.textSecondary)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Image(
                        painter = painterResource(id = R.mipmap.ic_into_grey),
                        contentDescription = ""
                    )
                }
                Divider(
                    color = AppTheme.colors.divider,
                    thickness = 1.dp,
                    modifier = Modifier.padding(start = 16.dp, end = 16.dp)
                )

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp), verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = getString(R.string.userinfo_email),
                        modifier = Modifier.weight(1f)
                    )
                    Text(
                        text = userEmail.value,
                        style = MaterialTheme.typography.bodyMedium.copy(color = AppTheme.colors.textSecondary)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Image(
                        painter = painterResource(id = R.mipmap.ic_into_grey),
                        contentDescription = ""
                    )
                }
                Divider(
                    color = AppTheme.colors.divider,
                    thickness = 1.dp,
                    modifier = Modifier.padding(start = 16.dp, end = 16.dp)
                )

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp), verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = getString(R.string.userinfo_pass),
                        modifier = Modifier.weight(1f)
                    )

                    Text(
                        text = getString(resourceId = R.string.userinfo_edit_pass),
                        style = MaterialTheme.typography.bodyMedium.copy(color = AppTheme.colors.primaryBtnBg),
                        modifier = Modifier.clickable(
                            indication = null,
                            interactionSource = remember { MutableInteractionSource() },
                            onClick = {
                                showChangePass.value = true
                            })
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Image(
                        painter = painterResource(id = R.mipmap.ic_into_grey),
                        contentDescription = ""
                    )
                }
            }
        }
    }

    if(showChangePass.value)
        BottomUpdatePassDialog(state = showChangePass, navigator = navigation){
            showChangePass.value = false
        }
}

@Preview(showBackground = true)
@Composable
fun MyUserInfoPrev() {
    AppTheme(false) {
        UserInfoScreen(EmptyDestinationsNavigator)
    }
}